package top.haijunit.work.modules.access.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.haijunit.work.modules.access.entity.RolePermissionEntity;
import top.haijunit.work.modules.access.mapper.RolePermissionMapper;

import java.util.Map;

/**
 * @author zhanghaijun
 * @description 针对表【access_role_permission(角色+权限表)】的数据库操作Service实现
 */
@Service
public class RolePermissionService extends ServiceImpl<RolePermissionMapper, RolePermissionEntity> {


    /**
     * 更新角色的权限配置
     */
    public void updateRolePermission(Long roleId, Map<Long, Integer> permissionMap) {
        LambdaQueryWrapper<RolePermissionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermissionEntity::getRoleCode, roleId);
        this.remove(queryWrapper);
        for (Map.Entry<Long, Integer> entry : permissionMap.entrySet()) {
            RolePermissionEntity rolePermissionEntity = new RolePermissionEntity();
            rolePermissionEntity.setRoleCode(roleId);
            rolePermissionEntity.setPermissionCode(entry.getKey());
            rolePermissionEntity.setStatusCode(entry.getValue());
            baseMapper.insert(rolePermissionEntity);
        }
    }
}




